package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.service.CarServiceZYH;

import net.sf.json.JSONArray;

public class AssignCarActionZYH  extends ActionSupport implements ServletResponseAware,ServletRequestAware{
	private HttpServletRequest req;
	private HttpServletResponse resp;
	@Resource
	private CarServiceZYH carservice;
	
	public void showCombo(){
		try {
			req.setCharacterEncoding("utf8");
			resp.setCharacterEncoding("utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONArray ja = JSONArray.fromObject(carservice.getDpidAndDpname());
		try {
			PrintWriter pw = resp.getWriter();
			pw.write(ja.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void assignCar2() throws IOException{
		try {
			req.setCharacterEncoding("utf8");
			resp.setCharacterEncoding("utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int carId = Integer.parseInt(req.getParameter("carId"));
		String dpName = req.getParameter("newdpName");
		boolean b = carservice.updateDpByCarId(carId, dpName);
		PrintWriter pw = resp.getWriter();
		if(b==true){
			pw.write("0");
		}
		
	}
	
	
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req = arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp = arg0;
	}

}
